<style scoped>
  .app-container >>> .scode {
    cursor: pointer;
    color: #20B2AA;
    text-decoration: underline;
  }
  .app-container >>> .scodeth {
    color: #515a6e;
  }
</style>

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="search">
        <el-input
          v-model="queryParams.search"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调出组织" prop="orgid">
        <el-select size="small" placeholder="请选择调出组织" clearable filterable v-model="queryParams.orgid">
          <el-option v-for="item in orgs" :label="item.name" :value="item.guid" :key="item.guid"/>
        </el-select>
      </el-form-item>
      <el-form-item label="调出库房" prop="fromsrguid">
        <el-select v-model="queryParams.fromsrguid" placeholder="请选择调出库房" filterable clearable size="small">
          <el-option
            v-for="item in roomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="调入组织" prop="toorgid">
        <el-select v-model="queryParams.toorgid" placeholder="请选择调入组织" filterable clearable size="small">
          <el-option v-for="item in orgs" :label="item.name" :value="item.guid" :key="item.guid"/>
        </el-select>
      </el-form-item>
      <el-form-item label="调入库房" prop="tosrguid">
        <el-select v-model="queryParams.tosrguid" placeholder="请选择调入库房" filterable clearable size="small">
          <el-option
            v-for="item in toRoomOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申请人" prop="applyname">
        <el-input
          v-model="queryParams.applyname"
          placeholder="请输入申请人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请日期">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.beginTime" type="date" value-format="yyyy-MM-dd" placeholder="选择开始日期">
        </el-date-picker>
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd" placeholder="选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:transferapply:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:transferapply:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transferapplyList" border @selection-change="handleSelectionChange" :span-method="objectSpanMethod">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="code"  label-class-name="scodeth" class-name="scode">
        <template slot-scope="scope">
          <span @click="viewIt(scope.row.id)">{{ scope.row.code }}</span>
        </template>
      </el-table-column>
      <el-table-column label="调出库房" align="center" prop="fromsrname"/>
      <el-table-column label="调出组织" align="center" prop="orgname" width="150"/>
      <el-table-column label="调入库房" align="center" prop="tosrname"/>
      <el-table-column label="调入组织" align="center" prop="toorgname"/>
<!--      <el-table-column label="发货重量" align="center" prop="loadweight">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ toFix6(scope.row.loadweight) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="到货重量" align="center" prop="unloadweight">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ toFix6(scope.row.unloadweight) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="物料名称" align="center" prop="itemname" width="150" />
      <el-table-column label="出库重量" align="center" prop="itemloadweight">
        <template slot-scope="scope">
          <span>{{ toFix6(scope.row.itemloadweight) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库重量" align="center" prop="itemunloadweight">
        <template slot-scope="scope">
          <span>{{ toFix6(scope.row.itemunloadweight) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备注" align="center" prop="itemremark" width="180" />-->
      <el-table-column label="状态" align="center" prop="statusname" />
      <el-table-column label="申请人" align="center" prop="applyname" />
      <el-table-column label="申请时间" align="center" prop="createtime">
        <template slot-scope="scope">
            <span>{{ scope.row.createtime }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库房调拨对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransferapply, getTransferapply, delTransferapply, addTransferapply, updateTransferapply, exportTransferapply } from "@/api/bill/transferapply";
import {findStoreroom,findOrganizationList} from "@/api/public";
import Print from 'vue-print-nb'

export default {
  name: "Transferapply",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库房调拨表格数据
      transferapplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 调拨类型字典
      typeOptions: [],
      roomOptions: [],
      toRoomOptions:[],
      dateRange: [],
      orgs:[],
      // 查询参数
      queryParams: {
        search: '',
        flowid: 28,
        pageNum: 1,
        pageSize: 10,
        fromsrguid: null,
        tosrguid: null,
        applyname: null,
        status: null,
        type: 1,
        createtime: null,
        beginTime: null,
        endTime: null,
        orgid:null,
        toorgid:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      OrderIndexArr: [],
      currType: 1
    };
  },
  created() {
    this.queryParams.beginTime = this.getMonthFirDate();
    this.getList();
    // this.findStoreroom();
    this.getDicts("transapply_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("transapply_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getOrgList();
  },
  watch:{
    "queryParams.orgid":function(val,oldVal){
      this.findStoreroom(val)
    },
    "queryParams.toorgid":function(val,oldVal){
      this.toFindStoreRoom(val);
    }
  },
  methods: {
    getOrgList(){
      findOrganizationList().then(response=>{
        this.orgs=response.data;
      })
    },
    viewIt(id) {
      this.$router.push({path: '/transfer/transferinfo', query: {id: id}});
    },
    // 获取相同编号的订单
    gatherTableData() {
      let OrderObj = {}
      this.transferapplyList.forEach((element, index) => {
        element.rowIndex = index
        if (OrderObj[element.code]) {
          OrderObj[element.code].push(index)
        } else {
          OrderObj[element.code] = []
          OrderObj[element.code].push(index)
        }
      })

      // 将数组长度大于1的值 存储到this.OrderIndexArr（也就是需要合并的项）
      this.OrderIndexArr = [];
      for (let k in OrderObj) {
        if (OrderObj[k].length > 1) {
          this.OrderIndexArr.push(OrderObj[k])
        }
      }
    },
    // 合并单元格
    objectSpanMethod({row,column,rowIndex,columnIndex}) {
      let colIndexs = [0,1,2,3,4,5,9,10,11];
      if (colIndexs.indexOf(columnIndex) > -1) {
        for (let i = 0; i < this.OrderIndexArr.length; i++) {
          let element = this.OrderIndexArr[i]
          for (let j = 0; j < element.length; j++) {
            let item = element[j]
            if (rowIndex == item) {
              if (j == 0) {
                return {
                  rowspan: element.length,
                  colspan: 1
                }
              } else if (j != 0) {
                return {
                  rowspan: 0,
                  colspan: 0
                }
              }
            }
          }
        }
      }
    },
    findStoreroom(guid) {
      this.queryParams.fromsrguid=null;
      findStoreroom({orgguid:guid}).then(response => {
        this.roomOptions = response.data;
      });
    },
    toFindStoreRoom(guid){
      this.queryParams.tosrguid=null;
      findStoreroom({orgguid:guid}).then(response=>{
        this.toRoomOptions=response.data;
      })
    },
    strtype: function () {
      if (this.currType == 1) {
        return true;
      } else {
        return false;
      }
    },
    /** 查询库房调拨列表 */
    getList() {
      this.loading = true;
      // this.addDateRange(this.queryParams, this.dateRange);
      listTransferapply(this.queryParams).then(response => {
        this.transferapplyList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.gatherTableData();
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 调拨类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        code: null,
        wlcode: null,
        fromsrguid: null,
        fromsrname: null,
        tosrguid: null,
        tosrname: null,
        applyname: null,
        orgid: null,
        orgname: null,
        shiftweight: null,
        loadweight: null,
        unloadweight: null,
        status: null,
        type: null,
        remark: null,
        flowid: null,
        operatorid: null,
        createtime: null,
        lastupdate: null,
        departid: null,
        departname: null,
        toorgid: null,
        toorgname: null,
        issendcar: null,
        iscopy: null,
        quality: null,
        beginTime: null,
        endTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      //控制列表展示字段
      this.currType = this.queryParams.type;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库房调拨";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getTransferapply(guid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改库房调拨";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            updateTransferapply(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTransferapply(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTransferapply(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库房调拨数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTransferapply(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
